﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClimateChangeChampionsData;

public partial class Templates_ccPupilNumbers_Default : BaseTemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AcmsContext.EnsureLoggedIn(this);
            foreach (Acms.Bll.Post child in AcmsContext.CurrentPost.Children)
            {
                if (child.Name.ToLower() == "wasteaudit")
                {
                    lnkWasteAudit.NavigateUrl = child.Url;
                }
                else if (child.Name.ToLower() == "energyaudit")
                {
                    lnkEnergyAudit.NavigateUrl = child.Url;
                }
            }
            DataBind();
        }
    }

    public override void DataBind()
    {
        pnlEntry.Visible = false;
        pnlMenuEnabled.Visible = false;
        pnlMenuDisabled.Visible = true;

        ClimateChangeChampionsData.DataContext context = new ClimateChangeChampionsData.DataContext();
        if (ClimateChangeChampionsData.CarbonCalculation.HasCarbonCalculation(context, User.Identity.User.Id, 1))
        {
            ClimateChangeChampionsData.CarbonCalculation calc = ClimateChangeChampionsData.CarbonCalculation.GetByUserIdAndAudit(context, User.Identity.User.Id, 1);
            if (calc != null && calc.NoPupils>0)
            {
                txtPupilNumber.Text = calc.NoPupils.ToString();
                pnlEntry.Visible = pnlMenuEnabled.Visible = true;
                pnlMenuDisabled.Visible = false;
            }
        }
        base.DataBind();
    }

    protected void btnEnter_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            ClimateChangeChampionsData.DataContext context = new ClimateChangeChampionsData.DataContext();
            ClimateChangeChampionsData.CarbonCalculation calc = ClimateChangeChampionsData.CarbonCalculation.GetByUserIdAndAudit(context, User.Identity.User.Id, 1);
            if (calc == null)
            {
                calc = new CarbonCalculation();
                calc.UserId = User.Identity.User.Id;
                calc.Audit = 1;
                context.CarbonCalculations.InsertOnSubmit(calc);
            }

            calc.NoPupils = int.Parse(txtPupilNumber.Text.Trim());

            context.SubmitChanges();
            DataBind();
        }
    }

}
